//package com.example.demo.scheduled;
//
//import jakarta.annotation.PostConstruct;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.sql.DataSource;
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//
//@Component
//public class DatabaseService {
//    private final DataSource dataSource;
//
//    public DatabaseService(DataSource dataSource) {
//        this.dataSource = dataSource;
//    }
//
//    @PostConstruct
//    private void init() {
//    }
//
//    @Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
//    public void cleanDatabase() {
//        String deleteQuery = "DELETE FROM user_diet WHERE create_time <= CURDATE() - INTERVAL 30 DAY";
//
//        try (Connection conn = dataSource.getConnection();
//             PreparedStatement stmt = conn.prepareStatement(deleteQuery)) {
//
//            int rowsAffected = stmt.executeUpdate();
//            System.out.println(rowsAffected + " rows deleted");
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}

